package cn.dubito.demo.listviewtest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.dubito.demo.listviewtest.adapter.FruitAdapter;
import cn.dubito.demo.listviewtest.domain.Fruit;

public class MainActivity extends AppCompatActivity {

    private List<Fruit> fruitList = new ArrayList<>();
//    private String[] data = {"Apple","Banana","Orange","Watermelon","Pear","Grape"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initFruits();
        FruitAdapter adapter = new FruitAdapter(MainActivity.this, R.layout.fruit_itme, fruitList);
        ListView listView = findViewById(R.id.list_view);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fruit fruit = fruitList.get(position);
                Toast.makeText(MainActivity.this,fruit.getName(),Toast.LENGTH_SHORT).show();
            }
        });
//        ArrayAdapter<String>  adapter = new ArrayAdapter<String>(MainActivity.this,
//                android.R.layout.simple_list_item_1,data);
//        ListView listView = findViewById(R.id.list_view);
//        listView.setAdapter(adapter);

    }

    private void initFruits() {

        for (int i = 0; i < 30; i++) {
            Fruit fruit = new Fruit("Apple", R.drawable.ic_launcher_background);
            fruitList.add(fruit);
        }
    }
}
